﻿Imports System.IO

Public Class SMSOut


#Region "Variables"

    Private strURL As String = "www.smsout.de/client/sendsms.php?"
    Private strUsername As String = Nothing
    Private strPassword As String = Nothing
    Private strPhoneNumber As String = Nothing
    Private intSMSType As Integer = 0
    Private strSMSText As String = Nothing
    Private strSendDateTime As String = Nothing
    Private strAltDisplayText As String = Nothing
    Private bHTTPS As Boolean = True
    Private arrParams As New ArrayList()
    Private Const strAccountData As String = "1"

#End Region

#Region "Properties"

    Public Property Username(Optional ByVal WithExtension As Boolean = True) As String
        Get
            Dim strReturn As String = Nothing
            If WithExtension = False Then
                strReturn = strUsername
            Else
                strReturn = "Username=" + strUsername
            End If
            Return strReturn
        End Get
        Set(ByVal value As String)
            strUsername = value
        End Set
    End Property

    Public Property Password(Optional ByVal WithExtension As Boolean = True) As String
        Get
            Dim strReturn As String = Nothing
            If WithExtension = False Then
                strReturn = strPassword
            Else
                strReturn = "Password=" + strPassword
            End If
            Return strReturn
        End Get
        Set(ByVal value As String)
            strPassword = value
        End Set
    End Property

    Public Property PhoneNumber(Optional ByVal WithExtension As Boolean = True) As String
        Get
            Dim strReturn As String = Nothing
            If WithExtension = False Then
                strReturn = strPhoneNumber
            Else
                strReturn = "SMSTo=" + strPhoneNumber
            End If
            Return strReturn
        End Get
        Set(ByVal value As String)
            strPhoneNumber = value
        End Set
    End Property

    Public Property SMSType(Optional ByVal WithExtension As Boolean = True) As String
        Get
            Dim strSMSType As String = Nothing
            Select Case intSMSType
                Case 0
                    strSMSType = "V1"
                Case 1
                    strSMSType = "V3"
                Case 2
                    strSMSType = "V4"
                Case 3
                    strSMSType = "V6"
            End Select
            If WithExtension Then
                strSMSType = "SMSType=" + strSMSType
            End If
            Return strSMSType
        End Get
        Set(ByVal value As String)
            intSMSType = value
        End Set
    End Property

    Public Property SMSText(Optional ByVal WithExtension As Boolean = True) As String
        Get
            Dim strReturn As String = Nothing
            If WithExtension = False Then
                strReturn = System.Web.HttpUtility.UrlEncode(strSMSText)
            Else
                strReturn = "SMSText=" + System.Web.HttpUtility.UrlEncode(strSMSText)
            End If
            Return strReturn
        End Get
        Set(ByVal value As String)
            strSMSText = value
        End Set
    End Property

    Public Property SendDateTime(Optional ByVal WithExtension As Boolean = True) As String
        Get
            Dim strReturn As String = Nothing
            If WithExtension = False Then
                strReturn = strSendDateTime
            Else
                strReturn = "SendDateTime=" + strSendDateTime
            End If
            Return strReturn
        End Get
        Set(ByVal value As String)
            strSendDateTime = value
        End Set
    End Property

    Public Property AltDisplayText(Optional ByVal WithExtension As Boolean = True) As String
        Get
            Dim strReturn As String = Nothing
            If WithExtension = False Then
                strReturn = strAltDisplayText
            Else
                strReturn = "AltDisplayText=" + strAltDisplayText
            End If
            Return strReturn
        End Get
        Set(ByVal value As String)
            strAltDisplayText = value
        End Set
    End Property

    Public Property HTTPS() As Boolean
        Get
            Return bHTTPS
        End Get
        Set(ByVal value As Boolean)
            bHTTPS = value
        End Set
    End Property

    Public ReadOnly Property URL() As String
        Get
            Dim strHUrl As String = Nothing
            If HTTPS Then
                strHUrl = "HTTPS://"
            Else
                strHUrl = "HTTP://"
            End If
            Return strHUrl + strURL
        End Get
    End Property

    Public ReadOnly Property GetAccountData(Optional ByVal WithExtension As Boolean = True)
        Get
            Dim strReturn As String = Nothing
            If WithExtension = False Then
                strReturn = strAccountData
            Else
                strReturn = "GetAccountData=" + strAccountData
            End If
            Return strReturn
        End Get
    End Property

#End Region

    Public Sub New(ByVal _Username As String, ByVal _Password As String, ByVal _PhoneNumber As String, ByVal _SMSType As Integer, ByVal _SMSText As String, Optional ByVal _HTTPS As Boolean = True, Optional ByVal _SendDateTime As String = Nothing, Optional ByVal _AltDisplayText As String = Nothing)
        Me.Username = _Username
        Me.Password = _Password
        Me.PhoneNumber = _PhoneNumber
        Me.SMSType = _SMSType.ToString
        Me.SMSText = _SMSText
        Me.HTTPS = _HTTPS
        Me.SendDateTime = _SendDateTime
        Me.AltDisplayText = _AltDisplayText
    End Sub

    Public Function Send()
        Dim bPost As Boolean = False
        Dim strGet As String = Nothing
        Add(Username)
        Add(Password)
        Add(PhoneNumber)
        Add(SMSType)
        Add(SMSText)
        If Not IsNothing(SendDateTime(False)) And SendDateTime(False) <> "" Then Add(SendDateTime)
        If Not IsNothing(AltDisplayText(False)) And AltDisplayText(False) <> "" Then Add(AltDisplayText)
        strGet += URL + GetParams()
        If SMSType(False) = "V6" Then bPost = True
        Return Request(strGet, bPost)
    End Function

    Public Function Info()
        Dim strGet As String = Nothing
        Add(Username)
        Add(Password)
        Add(GetAccountData)
        strGet += URL + GetParams()
        Return Request(strGet)
    End Function

    Private Sub Add(ByVal Value As String)
        Me.arrParams.Add(Value)
    End Sub

    Private Function GetParams() As String
        Dim strParams As String = Nothing
        For j As Integer = 0 To arrParams.Count - 1
            strParams += "&" + arrParams(j)
        Next
        Return strParams.Substring(1, strParams.Length - 1)
    End Function

    Private Function Request(ByVal _URL As String, Optional ByVal Post As Boolean = False)
        Dim responseContent As String = Nothing
        Dim webRequest As System.Net.HttpWebRequest
        Dim webResponse As System.Net.HttpWebResponse
        Try
            webRequest = CType(System.Net.WebRequest.Create(_URL), System.Net.HttpWebRequest)
            webResponse = CType(webRequest.GetResponse(), System.Net.HttpWebResponse)
            If Net.HttpStatusCode.OK Then
                Dim responseStream As System.IO.Stream = webResponse.GetResponseStream()
                Dim responseEncoding As System.Text.Encoding = System.Text.Encoding.UTF8
                Dim responseReader As New StreamReader(responseStream, responseEncoding)
                responseContent = responseReader.ReadToEnd()
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Return responseContent
    End Function

End Class
